home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / ply15dat.zip / TETRA.C < prev    next >
C/C++ Source or Header  |  1992-09-19  |  4KB  |  137 lines

  1. /*
  2.  * coil.c - Create a bunch of springs
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #ifdef MAC
  9. #include <console.h>
  10. #endif
  11. #include "def.h"
  12. #include "lib.h"
  13.  
  14. #define SIZE_FACTOR 5
  15.  
  16. static MATRIX trans;
  17.  
  18. /* Create tetrahedrons recursively */
  19. static void
  20. create_tetra(int depth, COORD4 *center)
  21. {
  22.     long    num_face, num_vert ;
  23.     COORD4  face_pt[3], obj_pt[4], sub_center, overt ;
  24.     long    swap, vert_ord[3] ;
  25.     long    x_dir, y_dir, z_dir ;
  26.  
  27.  
  28.     if ( depth <= 1 ) {
  29.         /* Output tetrahedron */
  30.  
  31.         /* find opposite corners of a cube which form a tetrahedron */
  32.         for ( num_vert = 0, x_dir = -1 ; x_dir <= 1 ; x_dir += 2 ) {
  33.             for ( y_dir = -1 ; y_dir <= 1 ; y_dir += 2 ) {
  34.                 for ( z_dir = -1 ; z_dir <= 1 ; z_dir += 2 ) {
  35.                     if ( x_dir*y_dir*z_dir == 1 ) {
  36.                         obj_pt[num_vert].x =
  37.                                         center->x + (double)x_dir * center->w ;
  38.                         obj_pt[num_vert].y =
  39.                                         center->y + (double)y_dir * center->w ;
  40.                         obj_pt[num_vert].z =
  41.                                         center->z + (double)z_dir * center->w ;
  42.                         ++num_vert ;
  43.                     }
  44.                 }
  45.             }
  46.         }
  47.  
  48.         /* find faces and output */
  49.         for ( num_face = 0 ; num_face < 4 ; ++num_face ) {
  50.             /* output order:
  51.              *   face 0:  points 0 1 2
  52.              *   face 1:  points 3 2 1
  53.              *   face 2:  points 2 3 0
  54.              *   face 3:  points 1 0 3
  55.              */
  56.             for ( num_vert = 0 ; num_vert < 3 ; ++num_vert ) {
  57.                 vert_ord[num_vert] = (num_face + num_vert) % 4 ;
  58.             }
  59.             if ( num_face%2 == 1 ) {
  60.                 swap = vert_ord[0] ;
  61.                 vert_ord[0] = vert_ord[2] ;
  62.                 vert_ord[2] = swap ;
  63.             }
  64.  
  65.             for ( num_vert = 0 ; num_vert < 3 ; ++num_vert ) {
  66.                 lib_transform_coord(&overt, &obj_pt[vert_ord[num_vert]], trans);
  67.                 COPY_COORD( face_pt[num_vert], overt) ;
  68.             }
  69.             lib_output_polygon(3, face_pt);
  70.         }
  71.     }
  72.  
  73.     else {
  74.         /* Create sub-tetrahedra */
  75.  
  76.         /* find opposite corners of a cube to form sub-tetrahedra */
  77.         for ( x_dir = -1 ; x_dir <= 1 ; x_dir += 2 ) {
  78.             for ( y_dir = -1 ; y_dir <= 1 ; y_dir += 2 ) {
  79.                 for ( z_dir = -1 ; z_dir <= 1 ; z_dir += 2 ) {
  80.                     if ( x_dir*y_dir*z_dir == 1 ) {
  81.                         sub_center.x =
  82.                                 center->x + (double)x_dir * center->w / 2.0 ;
  83.                         sub_center.y =
  84.                                 center->y + (double)y_dir * center->w / 2.0 ;
  85.                         sub_center.z =
  86.                                 center->z + (double)z_dir * center->w / 2.0 ;
  87.                         sub_center.w = center->w / 2.0 ;
  88.  
  89.                         create_tetra( depth-1, &sub_center) ;
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.     }
  95. }
  96.  
  97. void
  98. main(int argc, char *argv[])
  99. {
  100.     COORD4  back_color, tetra_color ;
  101.     COORD4  center_pt, light ;
  102.     COORD4  from, at, up, dir;
  103.  
  104.     MATRIX m1, m2;
  105.  
  106.    /* We are using Polyray */
  107.    lib_set_raytracer(OUTPUT_POLYRAY);
  108.  
  109.     lib_create_rotate_matrix(m1, Y_AXIS, PI/4.0);
  110.     lib_create_rotate_matrix(m2, Z_AXIS, 0.9553166181); /* acos(1/sqrt(3)) */
  111.     lib_matrix_multiply(trans, m1, m2);
  112.  
  113.     /* output viewpoint */
  114.     SET_COORD(from, 3.0, 0.0,-8.0) ;
  115.     SET_COORD(at,   0.0, 0.0, 0.0) ;
  116.     SET_COORD(up,   0.0, 1.0, 0.0) ;
  117.     lib_output_viewpoint( &from, &at, &up, 30.0, 1.0, 1.0, 256, 256);
  118.  
  119.     /* output background color - dark blue */
  120.     SET_COORD( back_color, 0.039, 0.18, 0.376 ) ;
  121.     lib_output_background_color( &back_color ) ;
  122.  
  123.     /* output light source */
  124.     SET_COORD4( light,-10.0, 20.0,-20.0, 0.7) ;
  125.     lib_output_light( &light ) ;
  126.     SET_COORD4( light, 10.0, 20.0, 20.0, 0.7) ;
  127.     lib_output_light( &light ) ;
  128.  
  129.     /* output tetrahedron color - red */
  130.     SET_COORD( tetra_color, 1.0, 0.2, 0.2 ) ;
  131.     lib_output_color(&tetra_color, 0.2, 0.8, 0.0, 0.0, 0.0, 0.0, 0.0) ;
  132.  
  133.     /* compute and output tetrahedral object */
  134.     SET_COORD4( center_pt, 0.0, 0.0, 0.0, 1.0 ) ;
  135.     create_tetra( SIZE_FACTOR, ¢er_pt) ;
  136. }
  137.